package model

import (
	"encoding/json"
	"errors"
	"time"

	"github.com/asaskevich/govalidator"
)

// 银行卡账目明细表
type BankTransactionDetails struct {
	ID                    int64     `json:"id" gorm:"id"`
	Date                  time.Time `json:"date" gorm:"date"`
	Type                  string    `json:"type" gorm:"type"`
	BankCard              string    `json:"bank_card" gorm:"bank_card"`
	Income                float64   `json:"income" gorm:"income"`
	Expenditure           float64   `json:"expenditure" gorm:"expenditure"`
	Balance               float64   `json:"balance" gorm:"balance"`
	Remark                string    `json:"remark" gorm:"remark"`
	AdditionalDescription string    `json:"additionalDescription" gorm:"additional_description"`
	CreateTime            time.Time `json:"createTime" gorm:"create_time"`
	UpdateTime            time.Time `json:"updateTime" gorm:"update_time"`

	// 同步标识
	IsIncomeExpenditure bool `json:"-" gorm:"-"`
}

func (btd *BankTransactionDetails) TableName() string {
	return "ams-bank_transaction_details"
}

func (btd *BankTransactionDetails) String() string {
	if dataInBytes, err := json.Marshal(&btd); err == nil {
		return string(dataInBytes)
	}

	return ""
}

func (btd *BankTransactionDetails) Validate() error {
	if btd.Date.IsZero() {
		return errors.New("the date of transaction must be specified")
	}

	if s := govalidator.Trim(btd.Type, ""); govalidator.IsNull(s) {
		return errors.New("the type of transaction must be specified")
	}

	if s := govalidator.Trim(btd.BankCard, ""); govalidator.IsNull(s) {
		return errors.New("the type of transaction must be specified")
	}

	if s := govalidator.Trim(btd.Remark, ""); govalidator.IsNull(s) {
		return errors.New("the type of transaction must be specified")
	}

	return nil
}
